package com.hishop.ware.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hishop.common.to.SkuHasStockVo;
import com.hishop.common.utils.PageUtils;
import com.hishop.common.utils.Query;
import com.hishop.ware.dao.WmsWareSkuDao;
import com.hishop.ware.entity.WmsWareSkuEntity;
import com.hishop.ware.service.WmsWareSkuService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wmsWareSkuService")
public class WmsWareSkuServiceImpl extends ServiceImpl<WmsWareSkuDao, WmsWareSkuEntity> implements WmsWareSkuService {

    @Resource
    private WmsWareSkuDao wareSkuDao;

//    @Resource
//    private ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<WmsWareSkuEntity> wmsWareSkuEntityQueryWrapper = new QueryWrapper<WmsWareSkuEntity>();
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            wmsWareSkuEntityQueryWrapper.eq("sku_id",skuId);
        }

        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wmsWareSkuEntityQueryWrapper.eq("ware_id",wareId);
        }

        IPage<WmsWareSkuEntity> page = this.page(
                new Query<WmsWareSkuEntity>().getPage(params),
                wmsWareSkuEntityQueryWrapper
        );

        return new PageUtils(page);
    }

    /**
     * 查出当前采购项的详细信息
     * @param skuId
     * @param wareId
     * @param skuNum
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {

        // 1、判读如果没有这个库存记录新增
        List<WmsWareSkuEntity> wareSkuEntities = wareSkuDao.selectList(new QueryWrapper<WmsWareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));

        if (wareSkuEntities == null || wareSkuEntities.size() == 0) {
            WmsWareSkuEntity wareSkuEntity = new WmsWareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStockLocked(0);
//            wareSkuEntity.setSkuName("");
//            //1、自己catch异常
//            try{
//                R info = productFeignService.info(skuId);
//                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
//                if (info.getCode() == 0) {
//                    wareSkuEntity.setSkuName((String) data.get("skuName"));
//                }
//            } catch (Exception e) {
//
//            }
//            //添加库存信息
//            wareSkuDao.insert(wareSkuEntity);
        } else {
            wareSkuDao.addStock(skuId,wareId,skuNum);
        }
    }

    @Override
    public List<SkuHasStockVo> getSkuHasStock(List<Long> skuIds) {
        List<SkuHasStockVo> skuHasStockVos = skuIds.stream().map(item -> {
            Long count = this.baseMapper.getSkuStock(item);
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            skuHasStockVo.setSkuId(item);
            skuHasStockVo.setHasStock(count == null?false:count > 0);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        return skuHasStockVos;
    }

}